package luj.tool.jnproxyan.proxy.util.data.session;

import luj.data.api.Data;
import luj.data.api.DataContext;
import luj.data.api.use.UseGroup;
import luj.tool.jnproxyan.proxy.database.DatabaseState;
import luj.tool.jnproxyan.proxy.network.socks.actor.root.Socks5Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DataUsingStarter {

  public interface Session {

    DatabaseState db();

    Map<String, Data> loaded();

    UseGroup using();
  }

  public DataUsingStarter inject(Socks5Actor actor) {
    return inject(actor.getDatabaseState());
  }

  public DataUsingStarter inject(DatabaseState dbState) {
    var result = new DataUsingStarter();
    result._dbState = dbState;
    result._lujdata = _lujdata;
    return result;
  }

  public Session start() {
    Map<String, Data> cache = new HashMap<>();
    UseGroup using = _lujdata.createUseGroup();

    return new Session() {
      @Override
      public DatabaseState db() {
        return _dbState;
      }

      @Override
      public Map<String, Data> loaded() {
        return cache;
      }

      @Override
      public UseGroup using() {
        return using;
      }
    };
  }

  DatabaseState _dbState;

  @Autowired
  DataContext _lujdata;
}
